package algorithm.algexercise.sort;

import edu.princeton.cs.algs4.Stopwatch;

import java.util.Arrays;

import static algorithm.Template.exch;
import static algorithm.Template.less;

/**
 * Created by chengxiaoqi1 on 2018/8/3.
 * 选择排序
 */
public class Selection {

    public static void sort(Comparable[] a) { // 将a[]按升序排列



        System.out.println(Arrays.asList(a));
        Stopwatch timer = new Stopwatch();
        int N = a.length; // 数组长度
        for (int i = 0; i < N; i++) { // 将a[i]和a[i+1..N]中最小的元素交换
            int min = i; // 最小元素的索引
            for (int j = i + 1; j < N; j++) {
                if (less(a[j], a[min])) {
                    min = j;
                }
            }
            exch(a, i, min);
        }
        System.out.println(timer.elapsedTime());
        System.out.println(Arrays.asList(a));
    }

    public static void main(String[] args) {
        Integer[] arr = new Integer[]{8,0,1,7,67,42};
        sort(arr);
    }

}

